package summer.dp;

public class Solution337 {
    public int rob(TreeNode root) {
        int[] res = robAct(root);
        return Math.max(res[0], res[1]);
    }

    public int[] robAct(TreeNode root) {
        if (root == null) return new int[]{0, 0};
        if (root.left == null && root.right == null) return new int[]{root.val, 0};
        int[] left = robAct(root.left);
        int[] right = robAct(root.right);
        return new int[]{root.val + left[1] + right[1], Math.max(left[0], left[1]) + Math.max(right[0], right[1])};
    }
}


class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}